/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import entities.Medium;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Lucyia
 */
public class MediumTableModel extends AbstractTableModel{
    private List<Medium> mediums = new ArrayList<Medium>();
    
    @Override
    public int getRowCount() {
        return mediums.size();
    }

    @Override
    public int getColumnCount() {
        return 2;
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Medium medium = mediums.get(rowIndex);
        
    switch (columnIndex) {
            case 0:
                return medium.isLended();
            case 1:
                return medium.getTitle();
            default:
                throw new IllegalArgumentException("Records table model doesn't support " + (columnIndex + 1) + " collumns");
        }
    }
      
    @Override
    public String getColumnName(int column){
        switch (column) {
            case 0:
                return "Lended";
            case 1:
                return "Title";
            default:
                throw new IllegalArgumentException("Records table model doesn't support " + (column + 1) + " collumns");
        }
    }  
        
    @Override
    public Class<?> getColumnClass(int columnIndex) {
        switch (columnIndex) {
            case 0:
                return Boolean.class;
            case 1:
                return String.class;
            default:
                throw new IllegalArgumentException("Records table model doesn't support " + (columnIndex + 1) + " collumns");
        }
    }
        
        @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        Medium medium = mediums.get(rowIndex);
        
        if (value == null)
            return;

        switch (columnIndex) {
            case 1:
                if (((String)value).isEmpty())  
                    return;
                medium.setLended(!medium.isLended());
                break;
            case 2:
                if (((String)value).isEmpty())  
                    return;
                medium.setTitle((String) value);
                break;
            default:
                throw new IllegalArgumentException();
        }

        fireTableCellUpdated(rowIndex, columnIndex);

    }
        
    public Medium getMediumAt(int i) {
        return mediums.get(i);
    }
    
    public void addMedium(Medium medium) {
        mediums.add(medium);
        fireTableDataChanged();
    }
    
    public void addMedia(List<Medium> allMedia) {
        mediums.clear();
        mediums.addAll(allMedia);
        fireTableDataChanged();
    }
    
    public void removeMedium(Medium medium) {
        mediums.remove(medium);
        fireTableDataChanged();
    }
    
    public void removeMedia(List<Medium> mediumsToRemove) {
        mediums.removeAll(mediumsToRemove);
        fireTableDataChanged();
    }
        
}
